package com.lanou.musicbox.adapter.localmusic;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.lanou.musicbox.R;
import com.lanou.musicbox.entity.Entity;
import com.lanou.musicbox.entity.EntityDao;
import com.lanou.musicbox.utils.fragment_back.DaoSingleton;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.dao.query.QueryBuilder;

/**
 * Created by 柳柏庆 on 15/10/20.
 * 本地音乐页面Adapter
 */

public class LocalityAdapter extends BaseAdapter {

    private Context context;        //上下文对象引用
    public static List<Entity> entities;   //存放Mp3Info引用的集合
    private Entity entity;        //Mp3Info对象引用
    private ViewContainer vc;
    private EntityDao entityDao;
    private DaoSingleton enDaoSingleton;
    private List<Entity> findlist = new ArrayList<>();


    public LocalityAdapter(Context context, List<Entity> mp3Infos) {
        this.context = context;
        this.entities = mp3Infos;
        entityDao = DaoSingleton.getInstance(context).getEntityDao();
        enDaoSingleton = DaoSingleton.getInstance(context);

    }


    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Object getItem(int position) {
        return entities != null && entities.size() > 0 ? entities.get(position) : 0;
    }

    @Override
    public int getCount() {
        return entities != null && entities.size() > 0 ? entities.size() : 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        vc = null;
        if (convertView == null) {
            vc = new ViewContainer();
            convertView = LayoutInflater.from(context).
                    inflate(R.layout.locality_item_music, null);
            vc.music_title = (TextView) convertView.findViewById(R.id.music_title);
            vc.music_artist = (TextView) convertView.findViewById(R.id.music_artist);
            vc.music_duration = (TextView) convertView.findViewById(R.id.music_duration);
            vc.song_list_num = (TextView) convertView.findViewById(R.id.song_list_num);
            vc.favorite_iv = (CheckBox) convertView.findViewById(R.id.favorite_iv);


            convertView.setTag(vc);
        } else {
            vc = (ViewContainer) convertView.getTag();
        }
        entity = entities.get(position);

        QueryBuilder<Entity> queryBuilder = enDaoSingleton.getEntityDao().queryBuilder();
            findlist = queryBuilder.where(EntityDao.Properties.Title.eq(entity.getTitle())).list();

        if (findlist.size() > 0) {
            vc.favorite_iv.setChecked(true);
            Log.i("llll", "输出" + findlist.size());
        } else {
            vc.favorite_iv.setChecked(false);
            Log.i("llll", "输出" + findlist.size());
        }





        vc.favorite_iv.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked == true) {
                    enDaoSingleton.getEntityDao().insert(entities.get(position));
                } else {
                    entityDao.queryBuilder().where(EntityDao.Properties.Title.eq(entities.get(position).getTitle())).buildDelete().executeDeleteWithoutDetachingEntities();
                }
            }
        });
        vc.music_title.setText(entity.getTitle());         //显示标题
        vc.music_artist.setText(entity.getArtist());       //显示艺术家
        vc.music_duration.setText(String.valueOf(formatTime(entity.getDuration()))); //显示长度
        vc.song_list_num.setText(position + 1 + ".");
        return convertView;
    }


    public static String formatTime(Long time) {                     //将歌曲的时间转换为分秒的制度
        String min = time / (1000 * 60) + "";
        String sec = time % (1000 * 60) + "";

        if (min.length() < 2)
            min = "0" + min;
        switch (sec.length()) {
            case 4:
                sec = "0" + sec;
                break;
            case 3:
                sec = "00" + sec;
                break;
            case 2:
                sec = "000" + sec;
                break;
            case 1:
                sec = "0000" + sec;
                break;
        }
        return min + ":" + sec.trim().substring(0, 2);
    }

    public class ViewContainer {

        private TextView music_title;
        private TextView music_artist;
        private TextView music_duration, song_list_num;
        private CheckBox favorite_iv;

    }


}
